home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / QuickDraw / Marquee ƒ / marquee.c next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  3.1 KB  |  145 lines  |  [TEXT/MPS ]

  1. /*
  2. ** James "im" Beninghaus
  3. */
  4.  
  5. /*
  6. ** MPW 3.0 C source to marquee!
  7. */
  8.  
  9. #include <QuickDraw.h>
  10. #include <Events.h>
  11. #include <Fonts.h>
  12. #include <Menus.h>
  13. #include <Windows.h>
  14. #include <Packages.h>
  15.  
  16. Resume() {
  17.     ExitToShell();
  18. }
  19.     
  20. main () {
  21.     auto    EventRecord        event;
  22.     auto    WindowRecord    window;
  23.     auto    Rect            bounds,
  24.                             rect;
  25.     auto    Point            start;
  26.     
  27.     /*
  28.     ** initialize the macintosh
  29.     */
  30.     InitGraf((Ptr) &qd.thePort);
  31.     InitFonts();
  32.     InitWindows();
  33.     InitMenus();
  34.     TEInit();
  35.     InitDialogs((ResumeProcPtr) Resume);
  36.     InitCursor();
  37.         
  38.     SetRect(&bounds, 50, 50, 500, 300);
  39.     NewWindow((Ptr)&window, &bounds, "\pApple Computer Inc.", true, noGrowDocProc, (WindowPtr)-1L, false, 0L);
  40.     SetPort((GrafPtr)&window);
  41.     
  42.     SetRect(&rect, 0, 0, 0, 0);
  43.     while (true) {
  44.         GetNextEvent(everyEvent, &event);
  45.         switch (event.what) {
  46.             case mouseDown :
  47.                 start = event.where;
  48.                 GlobalToLocal(&start);
  49.                 TrackMarquee(start, &rect);
  50.                 break;
  51.             case keyDown :
  52.                 ExitToShell();
  53.                 break;
  54.         }
  55.     }
  56. }
  57.  
  58. /*
  59. ** Description
  60. **        TrackMarquee will display a marquee similar to the 
  61. **        selection rectangle tool in MacPaint™. It is assumed that the
  62. **        current port has been set before calling. 
  63. **
  64. ** Parameters
  65. **        start        : the local coordinates where the mouse down occured.
  66. **        resultRect    : the final rectangle that was selected.
  67. */
  68.  
  69. #define    TICKDELAY    2
  70.  
  71. TrackMarquee(start, resultRect)
  72.     Point    start;
  73.     Rect    *resultRect;
  74. {    
  75.     /*
  76.     ** there are fifteen patterns defined here 
  77.     ** each one eight bytes long starting at :
  78.     ** patterns[0], patterns[1], patterns[2], patterns[3],
  79.     ** patterns[4], patterns[5], patterns[6], patterns[7]
  80.     */
  81.     static    unsigned char    patterns[] = {
  82.         0xF8, 0xF1, 0xE3, 0xC7, 0x8F, 
  83.         0x1F, 0x3E, 0x7C, 0xF8, 0xF1, 
  84.         0xE3, 0xC7, 0x8F, 0x1F, 0x3E
  85.     };
  86.     
  87.     auto        Point        mouse;        /* the current mouse location */
  88.     register    short        index;        /* the index of the current patterns array */
  89.     auto        Rect        nowRect,    /* the current rectangle to be framed */
  90.                             thenRect;    /* the last rectangle to be framed */
  91.     auto        long        nowTicks,    /* the current tick count */
  92.                             thenTicks;    /* the last tick count */
  93.     auto        PenState    penState;    /* the saved pen state on entry to procedure */
  94.  
  95.     
  96.     thenTicks = 0;
  97.     index = 0;
  98.     
  99.     GetPenState(&penState);
  100.     PenMode(patXor);
  101.     
  102.     PenPat(&patterns[index]);
  103.     SetRect(&nowRect, start.h, start.v, start.h, start.v);
  104.     FrameRect(&nowRect);
  105.     thenRect = nowRect;
  106.     
  107.     while (StillDown()) {
  108.         nowTicks = TickCount();
  109.         GetMouse(&mouse);
  110.         SetMobiusRect(&nowRect, start.h, start.v, mouse.h, mouse.v);
  111.         if (((thenTicks + TICKDELAY) < nowTicks ? thenTicks = nowTicks, true : false) || 
  112.         (!EqualRect(&nowRect, &thenRect))) {
  113.             FrameRect(&thenRect);
  114.             index = index < 7 ? index + 1 : 0;
  115.             PenPat(&patterns[index]);
  116.             FrameRect(&nowRect);
  117.             thenRect = nowRect;
  118.         }
  119.     }
  120.     FrameRect(&thenRect);
  121.     
  122.     SetPenState(&penState);
  123.     *resultRect = thenRect;
  124. }
  125.  
  126. SetMobiusRect(rect, left, top, right, bottom)
  127.     Rect    *rect;
  128.     short    left, top, right, bottom;
  129. {
  130.     if (left > right) {
  131.         rect->left = right;
  132.         rect->right = left;
  133.     } else {
  134.         rect->left = left;
  135.         rect->right = right;
  136.     }
  137.     if (top > bottom) {
  138.         rect->top = bottom;
  139.         rect->bottom = top;
  140.     } else {
  141.         rect->top = top;
  142.         rect->bottom = bottom;
  143.     }
  144. }
  145.